unit ServerMainForm;

interface

uses
  System.Actions, System.SysUtils, System.Classes, Vcl.Forms, Vcl.ActnList,
  Vcl.StdCtrls, Vcl.Controls, Vcl.ExtCtrls,

  WiRL.Core.Engine,
  WiRL.http.Server,
  WiRL.http.Server.Indy,
  WiRL.Core.Application;

type
  TMainForm = class(TForm)
    TopPanel: TPanel;
    StartButton: TButton;
    StopButton: TButton;
    MainActionList: TActionList;
    StartServerAction: TAction;
    StopServerAction: TAction;
    PortNumberEdit: TEdit;
    Label1: TLabel;
    procedure StartServerActionExecute(Sender: TObject);
    procedure StartServerActionUpdate(Sender: TObject);
    procedure StopServerActionExecute(Sender: TObject);
    procedure StopServerActionUpdate(Sender: TObject);
  strict private
    FServer: TWiRLServer;
  strict protected
    property Server: TWiRLServer read FServer;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

uses
  WiRL.Core.JSON,
  WiRL.Rtti.Utils,%MESSAGE_BODY_UNIT%
  ServerResources;

constructor TMainForm.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  PortNumberEdit.Text := '%SERVICE_PORT%';
  FServer := TWiRLServer.Create(Self);

  FServer.AddEngine<TWiRLEngine>('%ENGINE_PATH%')
    .SetEngineName('RESTEngine')
    .AddApplication('%APP_PATH%')
      .SetResources('*')
      .SetFilters('*');

//      .Plugin.Configure<IWiRLFormatSetting>
//        .AddFormat(TypeInfo(TDateTime), TWiRLFormatSetting.ISODATE_UTF)
//        .BackToApp
//
//      .Plugin.Configure<IWiRLConfigurationNeon>
//        .SetUseUTCDate(True)
//        .SetVisibility([mvPublic, mvPublished])
//        .SetMemberCase(TNeonCase.PascalCase);

  StartServerAction.Execute;
end;

destructor TMainForm.Destroy;
begin
  StopServerAction.Execute;
  FServer.Free;
  inherited Destroy;
end;

procedure TMainForm.StartServerActionExecute(Sender: TObject);
begin
  FServer.Port := StrToIntDef(PortNumberEdit.Text, %SERVICE_PORT%);
  if not FServer.Active then
    FServer.Active := True;
end;

procedure TMainForm.StartServerActionUpdate(Sender: TObject);
begin
  StartServerAction.Enabled := (FServer = nil) or (FServer.Active = False);
end;

procedure TMainForm.StopServerActionExecute(Sender: TObject);
begin
  FServer.Port := StrToIntDef(PortNumberEdit.Text, %SERVICE_PORT%);
  if not FServer.Active then
    FServer.Active := True;
end;

procedure TMainForm.StopServerActionUpdate(Sender: TObject);
begin
  StopServerAction.Enabled := Assigned(FServer) and FServer.Active;
end;

initialization
  ReportMemoryLeaksOnShutdown := True;

end.
